<?php

class WebinarsController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/admin/main';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login', 'logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','index','view', 'new', 'edit'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

  public function actionNew()
  {
    $members = Members::model()->findAll();
    $webinar=new Webinars;
    $this->render('new', array(
      'webinar' => $webinar,
      'members' => $members,
    ));
  }

  public function actionEdit()
  {
    $members = Members::model()->findAll();
    $webinar= Webinars::model()->find('id = :id', array(':id' => $_GET['id']));
    $this->render('edit', array(
      'webinar' => $webinar,
      'members' => $members,
    ));
  }

	public function actionCreate()
	{
		$model=new Webinars;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Webinars']))
		{
			$model->attributes=$_POST['Webinars'];
      $model->modified_at = date('Y-m-d H:i:s');
			if($model->save())
      {
        Yii::app()->user->setFlash('success', 'Success');
        Yii::app()->user->setFlash('content', 'You have successfully add new webinar.');

				$this->redirect(array('index'));
      }
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	public function actionUpdate()
	{
		if(isset($_POST['Webinars']))
		{
			$data=$_POST['Webinars'];
			$webinar=Webinars::model()->find('id = :id',array(':id' => $data['id']));
			$webinar->attributes=$data;
      $webinar->modified_at = date('Y-m-d H:i:s');

			if($webinar->save())
			{
				$this->redirect(array('index'));
			}

			$members = Members::model()->findAll();
			$this->render('edit',array(
				'webinar'=>$webinar,
				'members'=>$members,
			));
		}
	}

	public function actionDelete()
	{
		$webinar=Webinars::model()->find('id = :id', array(':id'=>$_GET['id']));
		$webinar->delete();
		$this->redirect(bu('admin/webinars/'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$webinars = Webinars::model()->with('member')->findAll();
		$this->render('index',array(
			'webinars' => $webinars,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Webinars::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='webinars-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
